package com.twu.biblioteca;

import java.io.IOException;

/**
 * Created by sahelig on 19/06/14.
 */
public class CheckOutMovie implements MenuAction {
    private Library libraryOnWhichOperationsArePerformed;
    private String stringToBeReturned;
    private boolean successful;
    private String actionPerformed;

    public CheckOutMovie(Library library) {
        libraryOnWhichOperationsArePerformed = library;
        actionPerformed = "to Check out a movie";
    }

    public Result performAction(UserInterface userInterfaceForApplication,String libraryNumber) throws IOException
    {

        String titleOfMovieToBeChecked = userInterfaceForApplication.takeUserInputForMovieTitle();
        String directorOfToBeCheckedOut=userInterfaceForApplication.takeUserInputForMovieDirector();
        String year=userInterfaceForApplication.takeUserInputForMovieYear();
        int rating=Integer.parseInt(userInterfaceForApplication.takeUserInputForMovieRating());

        Movie movieToBeCheckedOut = libraryOnWhichOperationsArePerformed.getMovieForInfo(titleOfMovieToBeChecked, directorOfToBeCheckedOut, year, rating);

        if (movieToBeCheckedOut == null)
        {
            userInterfaceForApplication.display("That movie is not available");
            stringToBeReturned = "Movie not present in catalogue";
            successful = false;
        }
        else

        if (libraryOnWhichOperationsArePerformed.checkOutMovie(movieToBeCheckedOut,libraryNumber)) {
            userInterfaceForApplication.display("Thank you! Enjoy the movie");
            stringToBeReturned = "Thank you! Enjoy the movie";
            successful = true;

        }
        else
        {
            userInterfaceForApplication.display("That movie is not available");
            stringToBeReturned = "That movie is not available";
            successful = false;
        }


        return new Result(successful, stringToBeReturned);
    }

    public String toString() {

        return actionPerformed;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        CheckOutMovie that = (CheckOutMovie) o;

        if (successful != that.successful) return false;
        if (actionPerformed != null ? !actionPerformed.equals(that.actionPerformed) : that.actionPerformed != null)
            return false;
        if (libraryOnWhichOperationsArePerformed != null ? !libraryOnWhichOperationsArePerformed.equals(that.libraryOnWhichOperationsArePerformed) : that.libraryOnWhichOperationsArePerformed != null)
            return false;
        if (stringToBeReturned != null ? !stringToBeReturned.equals(that.stringToBeReturned) : that.stringToBeReturned != null)
            return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = libraryOnWhichOperationsArePerformed != null ? libraryOnWhichOperationsArePerformed.hashCode() : 0;
        result = 31 * result + (stringToBeReturned != null ? stringToBeReturned.hashCode() : 0);
        result = 31 * result + (successful ? 1 : 0);
        result = 31 * result + (actionPerformed != null ? actionPerformed.hashCode() : 0);
        return result;
    }
}
